/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.tedu.mall.common.webapi.service.impl;

import cn.tedu.mall.common.pojo.leaf.common.Result;
import cn.tedu.mall.common.service.ISegmentService;
import cn.tedu.mall.common.webapi.IDGen;
import cn.tedu.mall.common.webapi.constant.Constants;
import cn.tedu.mall.common.webapi.exception.InitException;
import cn.tedu.mall.common.webapi.factory.PropertyFactory;
import cn.tedu.mall.common.webapi.id.ZeroIDGen;
import cn.tedu.mall.common.webapi.segment.SegmentIDGenImpl;
import cn.tedu.mall.common.webapi.segment.dao.IDAllocDao;
import cn.tedu.mall.common.webapi.segment.dao.impl.IDAllocDaoImpl;
import com.alibaba.druid.pool.DruidDataSource;
import java.util.Properties;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service("SegmentService")
@Slf4j
@DubboService
public class SegmentService implements ISegmentService, InitializingBean {
    @Value("${spring.datasource.url}")
    private String url;
    @Value("${spring.datasource.username}")
    private String username;
    @Value("${spring.datasource.password}")
    private String password;
    private IDGen idGen;
    private DruidDataSource dataSource;

    public Result getId(String key) {
        return idGen.get(key);
    }

    public SegmentIDGenImpl getIdGen() {
        if (idGen instanceof SegmentIDGenImpl) {
            return (SegmentIDGenImpl) idGen;
        }
        return null;
    }

    @Override public void afterPropertiesSet() throws Exception {
        Properties properties = PropertyFactory.getProperties();
        boolean flag = Boolean.parseBoolean(properties.getProperty(Constants.LEAF_SEGMENT_ENABLE, "true"));
        if (flag) {
            // Config dataSource
            dataSource = new DruidDataSource();
            dataSource.setUrl(url);
            dataSource.setUsername(username);
            dataSource.setPassword(password);
            dataSource.init();

            // Config Dao
            IDAllocDao dao = new IDAllocDaoImpl(dataSource);

            // Config ID Gen
            idGen = new SegmentIDGenImpl();
            ((SegmentIDGenImpl) idGen).setDao(dao);
            if (idGen.init()) {
                log.info("Segment Service Init Successfully");
            } else {
                throw new InitException("Segment Service Init Fail");
            }
        } else {
            idGen = new ZeroIDGen();
            log.info("Zero ID Gen Service Init Successfully");
        }
    }
}
